package com.atguigu.springbootmq.Controller;

import com.atguigu.springbootmq.config.DelayedQueueConfig;
import com.atguigu.springbootmq.config.TtlQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 发送延迟信息
 * 生产者
 */
@Slf4j //日志
@RestController
@RequestMapping("/ttl")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //开始发送消息
    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        log.info("当前时间：{},发送一条信息给两个TTl队列：{}",new Date().toString(),message);


        rabbitTemplate.convertAndSend("X", "XA", "消息来自 ttl 为 10S 的队列: "+message);
        rabbitTemplate.convertAndSend("X", "XB", "消息来自 ttl 为 40S 的队列: "+message);
//        rabbitTemplate.convertAndSend(TtlQueueConfig.X_EXCHANGE,"XA","消息来自10s的"+message);
//        rabbitTemplate.convertAndSend(TtlQueueConfig.X_EXCHANGE,"XB","消息来自40s的"+message);
    }
//开始发消息   和ttl
    @GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
    public void sendMsg(@PathVariable String message,@PathVariable String ttlTime){
        log.info("当前时间：{},发送一条时长：{}毫秒信息给TTl队列C：{}",new Date().toString(),ttlTime,message);
        rabbitTemplate.convertAndSend(TtlQueueConfig.X_EXCHANGE,"XC",message,msg->{
            //发送消息 延迟时长
            msg.getMessageProperties().setExpiration(ttlTime);
            return msg;
        });

    }

    //开始发消息 给予插件的 消息及延迟的时间
    @GetMapping("/sendDelayMsg/{message}/{delayTime}")
    public void sendMsg(@PathVariable String message,@PathVariable Integer delayTime) {
        log.info("当前时间：{},发送一条时长：{}毫秒信息给延迟队列delayed.queue{}", new Date().toString(), delayTime, message);
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME,DelayedQueueConfig.DELAYED_ROUTING_KEY,message, msg->{
            //发送消息 延迟时长 单位ms
            msg.getMessageProperties().setDelay(delayTime);
            return msg;
        });

    }







}
